﻿using NSoup.Nodes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor.Utils
{
    public class HtmlHelp
    {
        public static string 获取网页文本(string uri, string coding_name = "utf-8")
        {
            var wc = new WebClient();
            wc.Encoding = Encoding.GetEncoding(coding_name);
            return wc.DownloadString(uri);
        }

        public static 网页文档 获取网页文档(string uri, string coding_name = "utf-8")
        {
            var html = 获取网页文本(uri, coding_name);
            if (string.IsNullOrWhiteSpace(html)) return null;

            var doc = NSoup.NSoupClient.Parse(html);
            if (doc == null) return null;

            return new 网页文档(doc);
        }
    }

    public class HttpHelp
    {
        public static IronPython.Runtime.PythonTuple 发送请求(string url, IronPython.Runtime.PythonDictionary paramsDict)
        {
            return L.HttpGet(url, paramsDict);
        }

        public static IronPython.Runtime.PythonTuple 寄送请求(string url, IronPython.Runtime.PythonDictionary paramsDict)
        {
            return L.HttpPost(url, paramsDict);
        }
    }

    public class 网页文档
    {
        public 网页文档(NSoup.Nodes.Document document)
        {
            this._文档体 = document;
        }

        private NSoup.Nodes.Document _文档体;

        public NSoup.Nodes.Document 文档体 { get => _文档体; }

        public string 网页格式化文本
        {
            get
            {
                if (_文档体 == null) return null;

                return _文档体.OuterHtml();
            }
        }

        public IronPython.Runtime.PythonList 按名称取元素集(string tagName)
        {
            if (_文档体 == null) return null;

            var elements = _文档体.GetElementsByTag(tagName);
            if (elements == null || elements.Count <= 0) return null;

            var list = new IronPython.Runtime.PythonList();

            foreach (var element in elements)
            {
                list.Add(new 网页元素(element));
            }
            return list;
        }

        public 网页元素 按标识符取元素(string id)
        {
            if (_文档体 == null) return null;

            var element = _文档体.GetElementById(id);
            if (element == null) return null;

            return new 网页元素(element);
        }

        public IronPython.Runtime.PythonList 按类名取元素集(string className)
        {
            if (_文档体 == null) return null;

            var elements = _文档体.GetElementsByClass(className);
            if (elements == null || elements.Count <= 0) return null;

            var list = new IronPython.Runtime.PythonList();

            foreach (var element in elements)
            {
                list.Add(new 网页元素(element));
            }
            return list;
        }

        public IronPython.Runtime.PythonList 按特性名称取元素集(string attrName)
        {
            if (_文档体 == null) return null;

            var elements = _文档体.GetElementsByAttribute(attrName);
            if (elements == null || elements.Count <= 0) return null;

            var list = new IronPython.Runtime.PythonList();

            foreach (var element in elements)
            {
                list.Add(new 网页元素(element));
            }
            return list;
        }

        public IronPython.Runtime.PythonList 按特性值取元素集(string attrName, string attrValue)
        {
            if (_文档体 == null) return null;

            var elements = _文档体.GetElementsByAttributeValue(attrName, attrValue);
            if (elements == null || elements.Count <= 0) return null;

            var list = new IronPython.Runtime.PythonList();

            foreach (var element in elements)
            {
                list.Add(new 网页元素(element));
            }
            return list;
        }
    }

    public class 网页元素
    {
        public 网页元素(NSoup.Nodes.Element element)
        {
            this._元素体 = element;
        }

        private NSoup.Nodes.Element _元素体;

        public NSoup.Nodes.Element 元素体
        {
            get => _元素体;
        }

        public string 元素内部文本
        {
            get
            {
                if (this._元素体 == null) return "";
                return this._元素体.Text();
            }
        }

        public string 元素文本
        {
            get
            {
                if (this._元素体 == null) return "";
                return this._元素体.OuterHtml();
            }
        }

        public string 取特性值(string attrName)
        {
            if (string.IsNullOrWhiteSpace(attrName)) return null;

            return this._元素体.Attr(attrName);
        }

        public 网页元素 置特性值(string attrName, string attrValue)
        {
            if (string.IsNullOrWhiteSpace(attrName)) return null;

            this._元素体.Attr(attrName, attrValue);
            return this;  // 方便链式调用。
        }

        public 网页特性 取网页特性(string attrName)
        {
            if (string.IsNullOrWhiteSpace(attrName)) return null;

            var attrs = this._元素体.Attributes;
            foreach (var attr in attrs)
            {
                if (attrName == attr.Key) return new 网页特性(attr);
            }

            return null;
        }

        public IronPython.Runtime.PythonList 按名称取元素集(string tagName)
        {
            if (_元素体 == null) return null;

            var elements = _元素体.GetElementsByTag(tagName);
            if (elements == null || elements.Count <= 0) return null;

            var list = new IronPython.Runtime.PythonList();

            foreach (var element in elements)
            {
                list.Add(new 网页元素(element));
            }
            return list;
        }

        public 网页元素 按标识符取元素(string id)
        {
            if (_元素体 == null) return null;

            var element = _元素体.GetElementById(id);
            if (element == null) return null;

            return new 网页元素(element);
        }

        public IronPython.Runtime.PythonList 按类名取元素集(string className)
        {
            if (_元素体 == null) return null;

            var elements = _元素体.GetElementsByClass(className);
            if (elements == null || elements.Count <= 0) return null;

            var list = new IronPython.Runtime.PythonList();

            foreach (var element in elements)
            {
                list.Add(new 网页元素(element));
            }
            return list;
        }

        public IronPython.Runtime.PythonList 按特性名称取元素集(string attrName)
        {
            if (_元素体 == null) return null;

            var elements = _元素体.GetElementsByAttribute(attrName);
            if (elements == null || elements.Count <= 0) return null;

            var list = new IronPython.Runtime.PythonList();

            foreach (var element in elements)
            {
                list.Add(new 网页元素(element));
            }
            return list;
        }

        public IronPython.Runtime.PythonList 按特性值取元素集(string attrName, string attrValue)
        {
            if (_元素体 == null) return null;

            var elements = _元素体.GetElementsByAttributeValue(attrName, attrValue);
            if (elements == null || elements.Count <= 0) return null;

            var list = new IronPython.Runtime.PythonList();

            foreach (var element in elements)
            {
                list.Add(new 网页元素(element));
            }
            return list;
        }
    }

    public class 网页特性
    {
        public 网页特性(NSoup.Nodes.Attribute attribute)
        {
            this._特性体 = attribute;
        }

        private NSoup.Nodes.Attribute _特性体;

        public NSoup.Nodes.Attribute 特性体 { get => _特性体; }

        public string 特性值
        {
            get
            {
                if (_特性体 == null)
                    throw new Exception("此网页特性实例的 _特性体 实例不存在！");

                return _特性体.Value;
            }
            set
            {
                if (_特性体 != null)
                    this._特性体.SetValue(value);
                else
                    throw new Exception("此网页特性实例的 _特性体 实例不存在！");
            }
        }
    }
}
